package com.kancy.drools.learning.call.remote;

import org.kie.api.KieServices;
import org.kie.api.command.Command;
import org.kie.api.command.KieCommands;
import org.kie.api.runtime.ExecutionResults;
import org.kie.server.api.marshalling.MarshallingFormat;
import org.kie.server.api.model.ServiceResponse;
import org.kie.server.client.KieServicesClient;
import org.kie.server.client.KieServicesConfiguration;
import org.kie.server.client.KieServicesFactory;
import org.kie.server.client.RuleServicesClient;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * KieServerClient
 * <p>
 *
 * @author: kancy
 * @date: 2020/1/20 12:47
 **/

public class KieServerClient {
    private static String URL = "http://10.139.60.81:8280/kie-server/services/rest/server";
    private static String USERNAME = "kieserver";
    private static String PASSWORD = "kieserver1!";
    private static String CONTAINER_ID = "com.myspace:hello";
    private static String SESSION_NAME = "hello-session";

    public static void main(String[] args) {
        KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(URL, USERNAME, PASSWORD);
        config.setMarshallingFormat(MarshallingFormat.JSON);
        config.setTimeout(30000L);


        KieServicesClient client = KieServicesFactory.newKieServicesClient(config);
        RuleServicesClient rules = client.getServicesClient(RuleServicesClient.class);

        KieCommands cmdFactory = KieServices.Factory.get().getCommands();
        List<Command<?>> commands = new LinkedList<>();
        commands.add(cmdFactory.newInsert(getDataMap(), "dataMap"));
        commands.add(cmdFactory.newFireAllRules());
        ServiceResponse<ExecutionResults> response =
                rules.executeCommandsWithResults(CONTAINER_ID, cmdFactory.newBatchExecution(commands, SESSION_NAME));
        ExecutionResults result = response.getResult();
        System.out.println(result.getValue("dataMap"));

    }

    private static Object getDataMap() {
        Map<String,Object> inputMap = new HashMap<>();
        return inputMap;
    }

}
